package com.bookcode.controller;

import com.bookcode.entity.Greet;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

@RestController
public class GreetController {
    private static final String TEMPLATE = "Hello, %s!";
    @RequestMapping("/greet")
    public HttpEntity<Greet> greet(
            @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        Greet greet = new Greet(String.format(TEMPLATE, name));
        greet.add(linkTo(methodOn(GreetController.class).greet(name)).withSelfRel());
        return new ResponseEntity<>(greet, HttpStatus.OK);
    }
}
